package com.jinyi.wwdeng.ui.login.impl

import com.jinyi.base.mvp.BaseModel
import com.jinyi.base.network.IRequestCallback
import com.jinyi.base.network.NetworkUtils
import com.jinyi.wwdeng.network.ApiService
import com.jinyi.wwdeng.network.HttpBaseParams.addCommonParams
import com.jinyi.wwdeng.ui.login.contract.LoginContract
import com.zhi.analysis.util.GoogleInstallUtil
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/28 11:17
 * @Annotation： Model层  接口实现类
 */
class LoginModelImpl(iCallback: LoginContract.Presenter): BaseModel<LoginContract.Presenter>(iCallback),LoginContract.Model {

    /**
     * 发送验证码
     */
    override fun sendSmsCodeRequest(mobile: String) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path",ApiService.API_PATH.API_PATH_SMS)
            put("mobile",mobile)
        }
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onSendSmsSuccess(data)
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onFailureData(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 登录
     */
    override fun loginRequest(mobile: String,code: String,directLoginFlag: String) {
        val httpUrl = ApiService.getHttpUrl(ApiService.API_URL.API_COMMON_URL)
        val params = JSONObject().addCommonParams().apply {
            put("path",ApiService.API_PATH.API_PATH_LOGIN)
            put("mobile",mobile)//手机号
            put("code",code)//验证码
            put("channelCode", GoogleInstallUtil.getInstallReferrer())//渠道号
            put("subChannelCode", GoogleInstallUtil.getInstallReferrer())//子渠道号
            put("directLoginFlag",directLoginFlag)//是否直接登录
        }
        NetworkUtils.post(httpUrl,params,object : IRequestCallback {
            //请求成功
            override fun onRequestSuccess(data: JSONObject) {
                callBack.onLoginSuccess(data)
            }
            //请求失败
            override fun onRequestError(data: JSONObject) {
                callBack.onFailureData(data.optString("errorMsg"))
            }
            //请求完成
            override fun onRequestFinish() {
            }
        })
    }
}